//
//  MainViewController.swift
//  LZGWB
//
//  Created by Leoi ZoengGit on 2016/12/16.
//  Copyright © 2016年 Leoi ZoengGit. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.addSubview(composeButton)
        
        let rect = composeButton.frame
        
        let width = tabBar.bounds.size.width / CGFloat(childViewControllers.count)
        
        composeButton.frame = CGRect(x: width * 2, y: 0, width: width, height: rect.height)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

//        tabBar.tintColor = UIColor.orange
        
//        addChildViewController()
    }
    
    @objc fileprivate func compseBtnClick(_ btn: UIButton) {
        ZGLog("点击了加号按钮")
    }
    
    fileprivate lazy var composeButton: UIButton = {
        () -> UIButton
        in
        
        // 1.创建按钮
        let btn = UIButton(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
        
        btn.addTarget(self, action: #selector(compseBtnClick), for: UIControlEvents.touchUpInside)
        
//        // 2.设置前景图片
//        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
//        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
//        
//        // 3.设置背景图片
//        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
//        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
//        
//        // 4.调整按钮尺寸
//        btn.sizeToFit()
//        
        return btn
    }()
    


    func addChildViewController() {
        
        // 1.获取JSON文件路径
        guard let jsonPath = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else {
            ZGLog("没有获取对应的文件路径")
            return
        }
        
        // 2.读取JSON文件中的内容
        guard let jsonData = try? Data(contentsOf: URL(fileURLWithPath: jsonPath)) else {
            ZGLog("没有获取json文件中数据")
            return
        }
        
        do {
            // 3.将Data转换成数组
            guard let anyObject = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) else {
                return
            }
            
            // 4.遍历字典,获取对应的信息
            guard let dictArray = anyObject as? [[String: AnyObject]] else {
                return
            }
            
            for dict in dictArray {
                // 4.1获取控制器显示的title
                guard let title = dict["title"] as? String else {
                    continue
                }
                
                // 4.2获取控制器对应的字符串
                guard let vcName = dict["vcName"] as? String else {
                    continue
                }
                
                // 4.3获取控制器的图标名称
                guard let imageName = dict["imageName"] as? String else {
                    continue
                }
                
                addChildViewController(vcName, title: title, imageName: imageName)
            }
        } catch {
            
            // 只要try对应的方法发生异常,就会执行catch{}中的代码
            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_name")
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")

        }
    }
    
    // 添加一个子控制器
    func addChildViewController(_ childControllerName: String?, title: String?, imageName: String? ) {
        
        // 1.动态获取命名空间
        guard let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
            ZGLog("获取命名空间失败")
            return
        }
        
        // 2.根据字符串创建对应的Class
        guard let ChildVcClass = NSClassFromString(nameSpace + "." + childControllerName!) else {
            ZGLog("没有获取字符串对应的Class")
            return
        }
        
        // 3.将对应的AnyObjec转换成控制器的类型
        guard let childVcType = ChildVcClass as? UIViewController.Type else {
            ZGLog("没有获取对应控制器的类型")
            return
        }
        
        // 4.创建对应的控制器
        let childVc = childVcType.init()
        
        // 5.设置控制器的属性
        childVc.title = title
        childVc.tabBarItem.image = UIImage(named: imageName!)
        childVc.tabBarItem.selectedImage = UIImage(named: imageName! + "_highlighted")
        
        // 6.添加导航控制器
        let childNav = UINavigationController(rootViewController: childVc)
        
        // 7.添加控制器
        addChildViewController(childNav)
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
